from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from django.contrib.auth.models import Permission, ContentType

from meiduo_admin.serializers.permissions import PermissionsSerializer
from meiduo_admin.utils import PageNum
from meiduo_admin.serializers.permissions import ContentTypeSerializer


class PermissionsView(ModelViewSet):
    serializer_class = PermissionsSerializer
    queryset = Permission.objects.all()
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    # 父类中没有权限类型表的操作，需要自己封装
    def content_type(self, request):
        """
        获取权限类型
        :param request:
        :return:
        """
        # 1 获取权限类型的所有数据
        data = ContentType.objects.all()

        # 2 序列化返回权限类型
        ser = ContentTypeSerializer(data, many=True)  # 初始化生成序列化器对象
        # ser.is_valid()
        return Response(ser.data)
